/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package src.view.system;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.SessionScoped;
import javax.faces.context.FacesContext;
import org.primefaces.event.SelectEvent;
import src.domain.security.Users;
import src.domain.system.Approval;
import src.services.CRUEDService;
import src.util.Constanta;
import src.view.security.LoginBean;

/**
 *
 * @author jay
 */
@ManagedBean
@SessionScoped
public class ApprovalBean {

    /**
     * Creates a new instance of ApprovalBean
     */
     private Users user;
     private List<Approval> approvals;
     private Approval approval;
     private boolean updateTab=false;
                            
    public ApprovalBean() {
        ambilUser();
        populateApproval();
    }

    private void populateApproval () {
        CRUEDService servis = new CRUEDService();
        Map<String, Object> filter = new HashMap<>();
        Map<Object, Object> subfilter = new HashMap<>();
        int recordcount;
        approvals = new ArrayList<>();
        
        //cust to approve
        filter.put("approvalUid", null);
        filter.put("suspended", "false");
        recordcount = servis.countAllSQLAdvance("select count(*) from Customer", filter, "");
        approvals.add(new Approval(1, "Anggota", "../master/customer/custapproval.xhtml", recordcount));
                
        //saving to approve
        filter.clear();
        filter.put("approvalUid", null);
        filter.put("closeDate", null);
        filter.put("closedBy", null);
        recordcount = servis.countAllSQLAdvance("select count(*) from Saving", filter, "");
        approvals.add(new Approval(2, "Pembukaan Simpanan", "../saving/savapproval.xhtml", recordcount));
        
        //saving depo to approve
        filter.clear();
        subfilter.put("<=", user.getUgroup().getSavMaxDeposit());
        filter.put("authorizedId", null);
        filter.put("savtrxglId", null);
        filter.put("amount", subfilter);
        recordcount = servis.countAllSQLAdvance("select count(*) from Savdepo", filter, "");
        approvals.add(new Approval(3, "Setoran Simpanan", "../saving/savdepoapproval.xhtml", recordcount));
        
        //saving wd to approve
        filter.clear();
        subfilter.clear();
        subfilter.put("<=", user.getUgroup().getSavMaxDeposit());
        filter.put("approvalUid", null);
        filter.put("savtrxglId", null);
        filter.put("amount", subfilter);
        recordcount = servis.countAllSQLAdvance("select count(*) from Savwd", filter, "");
        approvals.add(new Approval(4, "Penarikan Simpanan", "../saving/savwdapproval.xhtml", recordcount));
        
        //saving trx to approve
        filter.clear();
        subfilter.clear();
        subfilter.put("<=", user.getUgroup().getSavMaxDeposit());
        filter.put("approvalUid", null);
        filter.put("amount", subfilter);
        recordcount = servis.countAllSQLAdvance("select count(*) from Savtrx", filter, "");
        approvals.add(new Approval(5, "Transfer Simpanan", "../saving/savtrxapproval.xhtml", recordcount));
        
        //saving gl depo to approve
        filter.clear();
        subfilter.clear();
        subfilter.put("<=", user.getUgroup().getSavMaxDeposit());
        filter.put("authorizedId", null);
        filter.put("savtrxglId", "notnull");
        filter.put("amount", subfilter);
        recordcount = servis.countAllSQLAdvance("select count(*) from Savdepo", filter, "");
        approvals.add(new Approval(6, "Setoran OB Dari GL", "../saving/savdepoglapproval.xhtml", recordcount));
        
        //saving gl wd to approve
        filter.clear();
        subfilter.clear();
        subfilter.put("<=", user.getUgroup().getSavMaxDeposit());
        filter.put("approvalUid", null);
        filter.put("savtrxglId", "notnull");
        filter.put("amount", subfilter);
        recordcount = servis.countAllSQLAdvance("select count(*) from Savwd", filter, "");
        approvals.add(new Approval(7, "Penarikan OB Ke GL", "../saving/savwdglapproval.xhtml", recordcount));
        
        //bilyet to approve
        filter.clear();
        subfilter.clear();
        subfilter.put("<=", user.getUgroup().getSavMaxDeposit());
        filter.put("authorizedUid", null);
        filter.put("closeDate", null);
        filter.put("principleAmount", subfilter);
        recordcount = servis.countAllSQLAdvance("select count(*) from Bilyet", filter, "");
        approvals.add(new Approval(8, "Penempatan Deposito", "../term_deposit/bilyetapproval.xhtml", recordcount));
        
        //loan to approve
        filter.clear();
        filter.put("loanStatus", Constanta.LOANSTATUS_WORKSHEET);
        recordcount = servis.countAllSQLAdvance("select count(*) from Loan", filter, "");
        approvals.add(new Approval(9, "Verifikasi Pinjaman", "../loan/loanapproval.xhtml", recordcount));
        
        //loan to approve dd
        filter.clear();
        filter.put("loanStatus", Constanta.LOANSTATUS_APPROVED);
        recordcount = servis.countAllSQLAdvance("select count(*) from Loan", filter, "");
        approvals.add(new Approval(10, "Otorisasi Pencairan ", "../loan/loanddapproval.xhtml", recordcount));
        
    }

    public List<Approval> getApprovals() {
        return approvals;
    }

    public Approval getApproval() {
        return approval;
    }

    public void setApproval(Approval approval) {
        this.approval = approval;
    }
    
    public Users getUser() {
        return user;
    }

    public void setUser(Users user) {
        this.user = user;
    }

    public boolean isUpdateTab() {
        return updateTab;
    }

    public void setUpdateTab(boolean updateTab) {
        this.updateTab = updateTab;
    }
    
    private void ambilUser() {
        FacesContext context = FacesContext.getCurrentInstance();
        LoginBean logBean = (LoginBean) context.getApplication().evaluateExpressionGet(context, "#{loginBean}", LoginBean.class);
        this.user = logBean.getSelectedUser();
    }
    
    public void onRowSelected(SelectEvent event) {
        //RequestContext context = RequestContext.getCurrentInstance();  
        //context.update(":panelOttor");  
        updateTab=true;
    }
    
    public void updateTabOtor() {
        this.approval.setCount(this.approval.getCount()-1);
    }
}
